package com.xj.hk.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.xj.hk.entity.Sp;
import com.xj.hk.service.SpService;
import com.xj.util.FilePathUtil;
import com.xj.util.JsonUtil;


@Controller
@RequestMapping(value="/uploadUtils")
public class UploadUtils {
	
	
	@Autowired
	private SpService spService;		
	public void setSpService(SpService spService) {
		this.spService = spService;
	}
		
	@RequestMapping(value="/uploadHead.htm")
	@ResponseBody
	public String uploadHead(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest request) throws SecurityException, IOException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat upload=new SimpleDateFormat("yyyyMMddHHddssSSS");
		String name    =request.getParameter("name");
		String fileName="";
		String data="image/"+sdf.format(new Date());//保存文件夹 
		if(file.getSize()!=0){		
			String up="/upload/"+data;//保存文件夹
			String path = request.getSession().getServletContext().getRealPath(up); 
			path = path.replaceAll("\\\\", "/");
			String hz=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),file.getOriginalFilename().length());//后缀
			fileName = upload.format(new Date())+hz;//保存文件名  
			File targetFile = new File(path, fileName); 
			
			String realPath=data+"/"+fileName;
		
			Sp sp=new Sp();
			sp.setType(1);
			sp.setTitle(name);
			sp.setPath(realPath);					
			spService.save(sp);
			
			if(FilePathUtil.validateFileName(targetFile)){
			
				if(!targetFile.exists()){  
					targetFile.mkdirs();  
				}  
				//保存   
				try {  
					file.transferTo(targetFile);  										
				} catch (Exception e) {  
					e.printStackTrace();  
				} 
			
			}
		}
		String ops=request.getContextPath();
		Map<String,Object> map= new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "成功");
		Map<String,Object> maps= new HashMap<String, Object>();
		maps.put("src",  ops+"/upload/"+data+"/"+fileName);
		map.put("data", maps);
		return JsonUtil.javaToJsonStr(map);
	}
	
	@RequestMapping(value="/uploadImg.htm")
	@ResponseBody
	public String uploadImg(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest request) throws SecurityException, IOException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat upload=new SimpleDateFormat("yyyyMMddHHddssSSS");
		String paths="";
		String fileName="";
		String data="image/"+sdf.format(new Date());//保存文件夹 
		if(file.getSize()!=0){		
			String up="/upload/"+data;//保存文件夹
			String path = request.getSession().getServletContext().getRealPath(up); 
			path = path.replaceAll("\\\\", "/");
			String hz=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),file.getOriginalFilename().length());//后缀
			fileName = upload.format(new Date())+hz;//保存文件名  
			File targetFile = new File(path, fileName); 
									
			if(FilePathUtil.validateFileName(targetFile)){
			
				if(!targetFile.exists()){  
					targetFile.mkdirs();  
				}  
				//保存   
				try {  
					file.transferTo(targetFile);  										
				} catch (Exception e) {  
					e.printStackTrace();  
				} 
			
			}
		}
		String ops=request.getContextPath();
		Map<String,Object> map= new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "成功");
		Map<String,Object> maps= new HashMap<String, Object>();
		maps.put("src",  ops+"/upload/"+data+"/"+fileName);
		maps.put("title", "图片");
		map.put("data", maps);
		return JsonUtil.javaToJsonStr(map);
	}
}
